package com.xls.dormitoryservice.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xls.dormitoryservice.entity.Hygiene;
import com.xls.dormitoryservice.vo.HygieneVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
public interface HygieneMapper extends BaseMapper<Hygiene> {
    @Select("SELECT\n" +
            "\th.id,\n" +
            "\tr.id AS roomId,\n" +
            "\tr.building,\n" +
            "\t( SELECT name FROM building WHERE id = r.building) AS buildingDesc,\n" +
            "\tr.floor,\n" +
            "\t( SELECT name FROM building WHERE id = r.floor) AS floorDesc,\n" +
            "\tr.rno,\n" +
            "\th.grade,\n" +
            "\ts.id AS studentId,\n" +
            "\ts.sno,\n" +
            "\ts.name,\n" +
            "\th.check_time AS checkTime,\n" +
            "\th.remarks\n" +
            "FROM\n" +
            "\thygiene h LEFT JOIN student s ON h.student_id = s.id\n" +
            "\tLEFT JOIN room r ON h.room_id = r.id" +
            "\t${ew.customSqlSegment} ORDER BY h.check_time DESC")
    Page<HygieneVo> getHygieneVoPage(Page<HygieneVo> page, @Param(Constants.WRAPPER) QueryWrapper<Hygiene> wrapper);

    @Select("SELECT\n" +
            "\th.id,\n" +
            "\tr.id AS roomId,\n" +
            "\tr.building,\n" +
            "\t( SELECT name FROM building WHERE id = r.building) AS buildingDesc,\n" +
            "\tr.floor,\n" +
            "\t( SELECT name FROM building WHERE id = r.floor) AS floorDesc,\n" +
            "\tr.rno,\n" +
            "\th.grade,\n" +
            "\ts.id AS studentId,\n" +
            "\ts.sno,\n" +
            "\ts.name,\n" +
            "\th.check_time AS checkTime,\n" +
            "\th.remarks\n" +
            "FROM\n" +
            "\thygiene h LEFT JOIN student s ON h.student_id = s.id\n" +
            "\tLEFT JOIN room r ON h.room_id = r.id" +
            "\tWHERE h.id = #{id}")
    HygieneVo getHygiene(String id);

    @Select("SELECT\n" +
            "\t*\n" +
            "FROM\n" +
            "\thygiene\n" +
            "WHERE\n" +
            "\troom_id = #{roomId}  AND is_deleted = 0 \n" +
            "ORDER BY\n" +
            "\tcheck_time DESC\n" +
            "\tLIMIT 4")
    List<Hygiene> getHygieneByRoomId(String roomId);
}
